package com.play.ballen.me.vm

import androidx.lifecycle.MutableLiveData
import com.play.ballen.R
import com.play.ballen.me.entity.WithDrawInfo
import com.play.ballen.network.ApiRepository
import com.play.ballen.network.BallenRxHelper
import com.yuven.appframework.busevent.SingleLiveEvent
import com.yuven.appframework.network.RxSchedulerHelper
import com.yuven.appframework.util.addToDisposable
import com.yuven.baselib.component.viewmodel.BaseViewModel
import com.yuven.baselib.utils.Logger
import com.yuven.baselib.utils.ResourceUtil

class WithDrawVM : MoneyLogVM() {

    val withDrawInfo = MutableLiveData<WithDrawInfo>()
    val actionCommitSuccess = SingleLiveEvent<Void>()

    fun getWithDrawInfo() {
        model.getWithDrawInfo()
            .onErrorResumeNext(BallenRxHelper.HttpErrorHandleFunc())
            .compose(BallenRxHelper.transformResponse())
            .compose(RxSchedulerHelper.io_main())
            .subscribe({
                Logger.i("WithDrawVM.getWithDrawInfo success")
                withDrawInfo.value = it
            }, { throwable ->
                toastLiveData.value = throwable.message
                Logger.i("WithDrawVM.getWithDrawInfo error,msg = ${throwable.message}")
            }).addToDisposable(compositeDisposable)
    }

    fun commitWithDraw(password: String, address: String, price: String) {
        showDialog.setValue(true, ResourceUtil.getString(R.string.loading))
        val map = mutableMapOf<String, Any>()
        map["password"] = password
        map["address"] = address
        map["price"] = price
        model.commitWithDraw(map)
            .onErrorResumeNext(BallenRxHelper.HttpErrorHandleFunc())
            .compose(BallenRxHelper.transformResponse())
            .compose(RxSchedulerHelper.io_main())
            .doFinally {
                showDialog.setValue(false)
            }
            .subscribe({
                Logger.i("WithDrawVM.commitWithDraw success")
                actionCommitSuccess.call()
            }, { throwable ->
                toastLiveData.value = throwable.message
                Logger.i("WithDrawVM.commitWithDraw error,msg = ${throwable.message}")
            }).addToDisposable(compositeDisposable)
    }
}